
/*
 * Copyright 2008 Friendster Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Friendster.Service.Core;

namespace Friendster.Service
{
    /// <summary>
    /// Represents the widget methods of friendster Platform.
    /// </summary>
    public sealed class WidgetService
    {
        // Fields
        private string _session;
         
        // Methods
        /// <summary>
        /// Initialize the session for using the services.
        /// </summary>
        /// <param name="session"></param>
        internal WidgetService(string session)
        {
            this._session = session;
        }
        /// <summary>
        /// Gets the widget specified by widget id.
        /// </summary>
        /// <param name="instanceID"></param>
        /// <returns>Widget object</returns>
        public Widget GetWidget(string instanceID)
        {
            FriendsterRequest request = new FriendsterRequest(this._session);

            //request.Parameters.Add("instance_id", instanceID);
            FriendsterResponse response = request.InvokeMethod("GET", "widget/" + instanceID);
            if (!response.IsError)
            {
                ArrayList result = (ArrayList)response.Result;
                return new Widget((Hashtable)result[0]);
            }
            return null;
        }
        /// <summary>
        /// Sets a new widget or updates the widget specified by Id.
        /// </summary>
        /// <param name="instanceID"></param>
        /// <param name="Content"></param>
        /// <returns>Widget object</returns>
        public string SetWidget(string instanceID, string Content)
        {
            FriendsterRequest request = new FriendsterRequest(this._session);
            if(!string.IsNullOrEmpty(instanceID))
                request.Parameters.Add("instance_id", instanceID);
            if(!string.IsNullOrEmpty(Content))
                request.Parameters.Add("content",Content);
            FriendsterResponse response = request.InvokeMethod("POST", "widget/");
            if (!response.IsError)
            {
                ArrayList result = (ArrayList)response.Result;
                return result[1].ToString();
            }
            return null;
        }
    }
}
